package com.game.World.Logical;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Max
 * Class contain a information of some logical game square
 * Instance - matrix of chars;
 */
public class Room {
	
	public char instance[][];
	
	private int width;
	
	private int height;
	
	/**
	 * Default constructor 
	 * Width and height = 20
	 */
	public Room() {
		
		width = 20;
		height = 20;
		
		instance = new char[width][height];
		init();
	}
	
	/**
	 * Constructor with given sizes of field
	 * @param width - size of X
	 * @param heigth - size of Y
	 */
	public Room(int width, int heigth) {
		this.width = width;
		this.height = heigth;
		
		instance = new char[this.width][this.height];
		init();
	}
	
	
	public Room(String fileName) throws IOException {
		
		File file = new File(fileName);
		
		
		if (!file.exists()){
	        throw new FileNotFoundException(file.getName());
	    }
		
		List<String> list = new ArrayList<String>();
		try {
	  
	        BufferedReader in = new BufferedReader(new FileReader( file.getAbsoluteFile()));
	        try {
	     
	            String s;
	            while ((s = in.readLine()) != null)
	                list.add(s);
	        } finally {
	            in.close();
	        }
	    } catch(IOException e) {
	        throw new RuntimeException(e);
	    }
		
		width = list.get(0).length();
		height = list.size();
		
		instance = new char[width][height];
		
		for (int j=0;j<height;j++)
			for (int i=0;i<width;i++)
				instance[i][j] = list.get(j).charAt(i);
		
	}
	
	
	/**
	 * Fill blank field
	 */
	private void init() {
		for (int i = 0; i < width; i++ )
			for (int j = 0; j< height; j++ )
				instance[i][j] = ' ';
	}
	
	
	
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight(){
		return height;
	}
	
	
	
	
	/**
	 * Return a possibility to insert new room in this
	 * Return true if it is possible or false - if not
	 * Coordinates are the place where you wont to insert left top corner
	 * @param room - Room that your wont to enter
	 * @param x  -  coordinate X to insert
	 * @param y  -  coordinate Y to insert
	 */
	public boolean canBuild(Room room,int x,int y) {
		
		if (x+room.getWidth()>this.width||x<0)
			return false;
		if (y+room.getHeight()>this.height||y<0)
			return false;
		
		
		for(int j=y; j<y+room.getHeight(); j++)
			for(int i=x; i<x+room.getWidth(); i++)
			{
				if( instance[i][j]!=room.instance[i-x][j-y])
					if (instance[i][j]!=' ')
						return false;
			}
		return true;
	}
	
	
	/**
	 * Insert Room instance to the exist one
	 * !!!!Call boolean CanBuild() method before this 
	 * @param room - Room that your wont to enter
	 * @param x  -  coordinate X to insert
	 * @param y  -  coordinate Y to insert
	 */
	public void Build(Room room, int x, int y) {
		if (!canBuild(room,x,y))
			return;
		for(int j=y; j<y+room.getHeight(); j++)
			for(int i=x; i<x+room.getWidth(); i++)
				instance[i][j] = room.instance[i-x][j-y];
	}
	
	/**
	 * Simply test the look of chamber on the console
	 */
	public void print() {
		System.out.println("----------Room----------");
		for (int j=0; j<height ;j++)
		{
			for (int i=0; i<width ;i++)
				System.out.print(instance[i][j]);
			System.out.println();
		}		
	}
	
	
	
	public Room Cut(int x1,int x2,int y1,int y2) {
		
		if(x1 < 0)
			x1 = 0;
		if(x1 > width)
			x1 = width;
		
		if(x2 < 0)
			x2 = 0;
		if(x2 > width)
			x2 = width;
		
		if (y1 < 0)
			y1 = 0;
		if (y1 > height)
			y1 = height;
		
		if (y2 < 0)
			y2 = 0;
		if (y2 > height)
			y2 = height;
		
		if (x1>x2)
			x1 = x2;
		
		if (y1>y2)
			y1 = y2;
		
		Room room = new Room(x2-x1+1,y2-y1+1);
		
		for(int j = y1 ; j <= y2 ; j++ )
			for (int i = x1; i <=x2 ; i++) 
				room.instance[i-x1][j-y1] = this.instance[i][j];
		return room;
	}

}

























